package edu.tamu.cse.lenss.activity;

import java.io.File;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import edu.tamu.cse.lenss.Constants;
import edu.tamu.cse.lenss.R;
import edu.tamu.cse.lenss.utils.Logger;

public class LENSSTrackActivity extends Activity {
	private static final String TAG = LENSSTrackActivity.class.getSimpleName();
	
	//private SensorService service;
	private Button startBtn;
	private TextView statusTV;
	private Button testBtn;
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        startBtn = (Button)findViewById(R.id.startBtn);
        statusTV = (TextView)findViewById(R.id.statusTV);
        testBtn = (Button)findViewById(R.id.testBtn);
        
        initUI();
    }
    
    private void initUI(){
    	if(isServiceRunning("edu.tamu.cse.lenss.service.SensorService")){
    		startBtn.setText("Stop");
    		statusTV.setText("Recording...");
    	}
    	else{
    		startBtn.setText("Start");
    		statusTV.setText("Ready to record");
    	}
    	
    	startBtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// Start  SensorService
				/*bindService(new Intent(getBaseContext(), SensorService.class),
						mConnection, Context.BIND_AUTO_CREATE);*/
				Intent serviceIntent = new Intent();
				serviceIntent.setAction(Constants.SENSOR_SERVICE_ACTION);
				if(startBtn.getText().toString().contains("Start")){
					startService(serviceIntent);
					statusTV.setText("Recording...");
					startBtn.setText("Stop");
				}
				else{
					stopService(serviceIntent);
					statusTV.setText("Ready to start");
					startBtn.setText("Start");
				}
			}
    	});
    	
    	testBtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				File parentDir = new File(Environment.getExternalStorageDirectory(), 
						Constants.ROOT_DIRECTORY + "/" + Constants.PROJECT_NAME + "/" + "Acc");
				if(!parentDir.exists()){
					if(parentDir.mkdirs())
						Logger.v(TAG, "Directory created");
					else
						Logger.v(TAG, "Fail to create directory");
				}
				
			}
    	});
    }
    
    
    /*private final ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder iBinder) {
			service = ((SensorService.LocalBinder)iBinder).getService();
			Logger.v(TAG, "Servie " + className + " Conected");
			startBtn.setText("Recording...");
			startBtn.setText("Stop");
		}

		public void onServiceDisconnected(ComponentName className) {
			Logger.v(TAG, "Servie " + className + " disconected");
		}
    };*/
    
    /**
     * Check if the given service is running at the background
     * @param serviceName
     * @return
     */
    private boolean isServiceRunning(String serviceName) {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        	//Logger.i(TAG, "Discover Service: " + service.service.getClassName());
            if (serviceName.equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

}